let form = layui.form
$.ajax({
    url: "/my/article/cates",
    success: function (res) {
        if (res.status == 0) {
            let str = ` <option value = "" > 请选择文章类别</option>`
            res.data.forEach(item => {
                str += `<option value="${item.Id}">${item.name}</option>`
            })
            $("select").html(str)
            form.render("select")
        }
    }
})


tinymce.init({
    selector: '.layui-textarea',
    language: 'zh_CN',
});

$('#image').cropper({
    aspectRatio: 1,
    preview: '.img-preview'
});
$(".upload-btn").click(function () {
    $("#file").click();
});
$("#file").on("change", function () {
    let obj = this.files[0];
    let url = URL.createObjectURL(obj);
    $('#image').cropper("replace", url);
});
$("form").on("submit", function (e) {
    e.preventDefault();
    let fd = new FormData(this);
    fd.set("content", tinyMCE.activeEditor.getContent());
    let obj = $('#image').cropper('getCroppedCanvas', {
        width: 280,
        height: 280
    });
    obj.toBlob(function (blob) {
        fd.append("cover_img", blob);
        $.ajax({
            processData: false, //重要
            contentType: false, //重要
            url: "/my/article/add",
            type: "POST",
            data: fd,
            success: function (res) {
                if (res.status == 0) {
                    layer.msg(res.message);
                    window.parent.document.querySelector("#list").click();
                }
            }
        })

    });
})